﻿#include "randomstring.h"
#include <QtDebug>

QVector<QChar> RandomString::m_number({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
QVector<QChar> RandomString::m_lowercase({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'});
QVector<QChar> RandomString::m_uppercase({'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'});

RandomString::RandomString()
    :randomGenerator(randomDevice())
{
}

RandomString::RandomString(unsigned int seed)
    :randomGenerator(seed)
{

}

void RandomString::addCharSet(QString set)
{
    QString::const_iterator it;
    for (it = set.constBegin(); it != set.constEnd(); ++it)
    {
          addChar(*it);
    }
}

QString RandomString::randn(int N)
{
    int M = m_charSet.size();
    std::uniform_int_distribution<int> dist(0, M - 1);

    QString ret;

    while(N > 0)
    {
        int rand = dist(randomGenerator);
        QChar x = m_charSet.at(rand);
        //qDebug() << "rand = " << rand << ", x = " << x;
        ret.append(x);
        --N;
    }
    return ret;
}

void RandomString::resetCharSet()
{
    m_charSet.clear();
}

void RandomString::addChar(QChar c)
{
    m_charSet.append(c);
}

void RandomString::addCharSet(QVector<QChar> set)
{
    m_charSet.append(set);
}

void RandomString::addCharSet(CHARSET set)
{
    switch (set)
    {
    case number:
        m_charSet.append(m_number);
        break;
    case lowercase:
        m_charSet.append(m_lowercase);
        break;
    case uppercase:
        m_charSet.append(m_uppercase);
        break;
    default:
        break;
    }
}
